package org.example.cet.demos.controller;


import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.dto.ClassroomDto;
import org.example.cet.demos.domain.form.TeacherForm;
import org.example.cet.demos.domain.po.Classroom;
import org.example.cet.demos.domain.po.Teacher;
import org.example.cet.demos.domain.query.TeacherQuery;
import org.example.cet.demos.service.impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherServiceImpl teacherService;
    @Autowired
    private converter converter;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody TeacherForm teacherForm) {
        Teacher teacher=converter.ToTeacher(teacherForm);
        teacherService.save(teacher);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        teacherService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update/{id}")
    public Result update(@PathVariable Long id,@Valid @RequestBody TeacherForm teacherForm) {
        Teacher teacher=converter.ToTeacher(teacherForm);
        teacher.setId(id);
        teacherService.updateById(teacher);
        return Result.success();
    }

    @GetMapping("/getPage")
    public Result getPage(TeacherQuery teacherQuery) {
        PageBean pb=teacherService.getPage(teacherQuery);
        return Result.success(pb);
    }

    @GetMapping("/getList")
    public Result list(){
        return Result.success(teacherService.getList());
    }

    @GetMapping("/findClass/{id}")
    public Result findClass(@PathVariable Long id){
        List<ClassroomDto> classrooms=teacherService.findClass(id);
//
//        return Result.success(teacherService.findClass(id));
        return Result.success(classrooms);
    }
}
